<?php
namespace app\common\model;
use x\Xbase;
class Orderjifen extends Xbase
{
    /**
     * 积分记录
     * @param [type]  $user_id [description]
     * @param [type]  $money   [description]
     * @param integer $type    [
     *                0|获得注册积分，
     *                1|购物赠送积分
     *                2|分享产品积分
     *                3|推广注册积分
     *                4|购物抵扣积分
     *
     *                5|产品退款积分
     *                6|系统增加积分
     *                7|系统减少积分
     *                8|升级赠送积分
     * ]
     */
    public function add($order_no,$user_id,$money,$type=0){
        $data = [
            'order_no' => $order_no,
            'user_id' => $user_id,
            'type' => $type,
            'create_time' => time()
        ];
        if( $money > 0 ){
            $data['show_money']   = "+".bcadd($money,0,2);
            $data['money'] = $money;
        }else{
            $data['show_money']   = bcadd($money,0,2);
            $data['money'] = bcsub(0,$money,2);
        }
        $id = $this->db->insert('order_jifen',$data);
        if(!$id) return $this->setError('创建订单失败');
        if( $money > 0 ){
            $this->db->query("update user_ext set total_money_jf=total_money_jf+? where user_id=?",[$data['money'],$user_id]);
        }else{
            $this->db->query("update user_ext set use_money_jf=use_money_jf+? where user_id=?",[$data['money'],$user_id]);
        }
        return $id;
    }

}
